package com.bugbuff.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.bugbuff.dubbo.server.pojo.Logs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class HoutaiApiImpl implements  HoutaiApi {
@Autowired
private MongoTemplate mongoTemplate;

    @Override
    public int query7UserList(Long today) {

        Query query =new Query();
//        query.fields().include("loginTime");
        List logsList = mongoTemplate.find(query, Logs.class);

        List list =new ArrayList();
        Set set =new HashSet<>();
        for (int i = 0;i<logsList.size();i++){
            Logs o = (Logs) logsList.get(i);
            Long temp = o.getLoginTime() ;
           Long  result =today-temp;
            int x = 7*24*60*60*1000;
           if (result<=x){
               set.add(o.getUid());
           }
        }
        return set.size();

    }

    @Override
    public int queryUserList(long today) {
        Query query =new Query();
        //query.fields().include("loginTime");
        List logsList = mongoTemplate.find(query, Logs.class);
        List list =new ArrayList();
        Set set =new HashSet<>();
        for (int i = 0;i<logsList.size();i++){
            Logs o = (Logs) logsList.get(i);
            Long temp = o.getLoginTime() ;
            Long  result =today-temp;
            Long x = 30*24*60*60*1000L;
            if (result<=x){
                set.add(o.getUid());
            }
        }
        return set.size();
    }

    @Override
    public List<Logs> queryLoginList() {
        List<Logs> all = this.mongoTemplate.findAll(Logs.class);
        return all;
    }

    @Override
    public int queryTodayLoginList(long newday) {
        Query query =new Query();
        List logsList = mongoTemplate.find(query, Logs.class);
        List list =new ArrayList();
        Set set =new HashSet<>();
        for (int i = 0;i<logsList.size();i++){
            Logs o = (Logs) logsList.get(i);
            Long temp = o.getLoginTime() ;
            Long  result =temp-newday;

            if (result>=0){
                set.add(o.getUid());
            }
        }
        return set.size();
    }

    @Override
    public int queryYesterdayLoginList(long yesterday) {
        Query query =new Query();
        List logsList = mongoTemplate.find(query, Logs.class);
        List list =new ArrayList();
        Set set =new HashSet<>();
        for (int i = 0;i<logsList.size();i++){
            Logs o = (Logs) logsList.get(i);
            Long temp = o.getLoginTime() ;
            Long  result =temp-yesterday;

            if (result>=0&&result<=24*60*60*1000){
                set.add(o.getUid());
            }
        }
        return set.size();
    }

    @Override
    public int queryOldyesDayLoginList(Long oldyesday) {
        Query query =new Query();
        List logsList = mongoTemplate.find(query, Logs.class);
        List list =new ArrayList();
        Set set =new HashSet<>();
        for (int i = 0;i<logsList.size();i++){
            Logs o = (Logs) logsList.get(i);
            Long temp = o.getLoginTime() ;
            Long  result =temp-oldyesday;

            if (result<=86400000&&result>=0){
                set.add(o.getUid());
            }
        }
        return set.size();
    }
}
